//
//  UIBarButtonItem+Extension.swift
//  HMWeiboBySwift
//
//  Created by wuxl on 15/6/30.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    convenience init(imageName: String, highlightImageName: String?) {
        let btn = UIButton()
        btn.sizeToFit()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        // ?? 表示如果前面的内容为 nil，使用后面的内容
        let hImageName = highlightImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        
        self.init(customView: btn)
    }
    
    ///  创建 UIBarButtonItem
    ///  :param: imageName 图像名
    ///  :param: highlightImageName 高亮名 可以为 nil
    ///  :param: target target
    ///  :param: actionName actionName
    convenience init(imageName: String, highlightImageName: String?, target: AnyObject?, actionName: String?) {
        let btn = UIButton()
        btn.sizeToFit()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        let hImageName = highlightImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        
        // 添加监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        self.init(customView: btn)
    }
}
